package csu.rjxyg.action.infoAction;

import java.io.IOException;
import java.util.List;

import csu.rjxyg.action.BaseAction;
import csu.rjxyg.action.constant.InfoConstant;
import csu.rjxyg.action.constant.UserConstant;
import csu.rjxyg.bean.core.BaseObject;
import csu.rjxyg.dao.BaseDaoAdapter;
import csu.rjxyg.factory.core.DaoFactory;
import csu.rjxyg.factory.core.UtilFactory;
import csu.rjxyg.service.BaseInfoService;
import csu.rjxyg.service.BaseUserService;
import csu.rjxyg.util.LogUtil;

public class DeleteInfoAction extends BaseAction {
	private LogUtil log;
	
	private String[] inInfoIdList;
	private String[] postInfoIdList;
	
	public DeleteInfoAction(){
		log =  UtilFactory.getLogUtil(getClass());
	}
	
	public void wasteInInfo(){
		String name = null;
		try{
			BaseObject object = (BaseObject) this.getSession().get(UserConstant.USER_SESSION);
			name = (String) object.get(UserConstant.USER_ID);
			BaseInfoService infoService = (BaseInfoService) factory.getBean(BaseAction.INFO_SERVICE);
			if( infoService.changeInfoStatus(inInfoIdList, "废弃") ){
				log.info(name + "标记信息为废弃成功");
				this.out("success");
				return;
			}
			this.out("删除失败！请联系服务提供商");
		}catch(Exception e){
			log.info(name + "标记信息为废弃失败");
			e.printStackTrace();
			log.info(e.getMessage());
		}
	}
	
	public void wastePostInfo(){
		String name = null;
		try{
			BaseObject object = (BaseObject) this.getSession().get(UserConstant.USER_SESSION);
			name = (String) object.get(UserConstant.USER_ID);
			BaseInfoService infoService = (BaseInfoService) factory.getBean(BaseAction.INFO_SERVICE);
			if( infoService.changeInfoStatus(postInfoIdList, "废弃") ){
				log.info(name + "标记信息为废弃成功");
				this.out("success");
				return;
			}
			this.out("删除失败！请联系服务提供商");
		}catch(Exception e){
			log.info(name + "标记信息为废弃失败");
			e.printStackTrace();
			log.info(e.getMessage());
		}
	}
	
	public void deleteInInfo(){
		String name = null;
		try{
			BaseObject object = (BaseObject) this.getSession().get(UserConstant.USER_SESSION);
			name = (String) object.get(UserConstant.USER_ID);
			if( inInfoIdList.length == 0 ){
				this.out("error:请选择要删除的项！");
				return;
			}
			
			BaseUserService userService = (BaseUserService) factory.getBean(BaseAction.USER_SERVICE);
			for( int i = 0 ; i < inInfoIdList.length;i++ ){
				List infoList = (List) object.get(UserConstant.USER_ACCEPT_INFO);
				for(int j = 0 ; j < infoList.size(); j++){
					if( infoList.get(j).equals(inInfoIdList[i]) ){
						infoList.remove(j);
					}
				}
			}
			if( userService.updateUserBean(name, object)){
				log.info(name + "信息删除成功");
				this.out("success");
				return;
			}
			this.out("删除失败！请联系服务提供商");
		}catch(Exception e){
			log.info(name + "信息删除失败");
			e.printStackTrace();
			log.info(e.getMessage());
		}
	}
	
	public void deletePostInfo(){
		String name = null;
		try{
			BaseObject object = (BaseObject) this.getSession().get(UserConstant.USER_SESSION);
			name = (String) object.get(UserConstant.USER_ID);
			if( postInfoIdList.length == 0 ){
				this.out("error:请选择要删除的项！");
				return;
			}
			
			BaseUserService userService = (BaseUserService) factory.getBean(BaseAction.USER_SERVICE);
			for( int i = 0 ; i < postInfoIdList.length;i++ ){
				List infoList = (List) object.get(UserConstant.USER_SEND_INFO);
				for(int j = 0 ; j < infoList.size(); j++){
					if( infoList.get(j).equals(postInfoIdList[i]) ){
						infoList.remove(j);
					}
				}
			}
			if( userService.updateUserBean(name, object)){
				log.info(name + "信息删除成功");
				this.out("success");
				return;
			}
			this.out("删除失败！请联系服务提供商");
		}catch(Exception e){
			log.info(name + "信息删除失败");
			e.printStackTrace();
			log.info(e.getMessage());
		}
		
	}

	public String[] getInInfoIdList() {
		return inInfoIdList;
	}

	public void setInInfoIdList(String[] inInfoIdList) {
		this.inInfoIdList = inInfoIdList;
	}

	public String[] getPostInfoIdList() {
		return postInfoIdList;
	}

	public void setPostInfoIdList(String[] postInfoIdList) {
		this.postInfoIdList = postInfoIdList;
	}
	
	
	
	
}
